package com.yndsoft.jc.fragment;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.yndsoft.jc.R;
import com.yndsoft.jc.activity.BaseFragment;
import com.yndsoft.jc.model.Ad;
import com.yndsoft.jc.model.ProductType;
import com.yndsoft.jc.model.ReturnMsg;
import com.yndsoft.jc.ui.categorypage.ProductSearchActivity;
import com.yndsoft.jc.ui.categorypage.adapter.CategoryAdapter;
import com.yndsoft.jc.ui.categorypage.entity.BaseAddressItem;
import com.yndsoft.jc.ui.categorypage.entity.BaseListItem;
import com.yndsoft.jc.ui.net.util.ProgressSubscriber;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;

/**
 * Created by 80582 on 2017/11/28.
 */

public class CategoryFragment extends BaseFragment implements View.OnClickListener {
    private Activity context;
    private View root;

    @Bind(R.id.category_search)
    TextView category_search;
    @Bind(R.id.listView_category)
    ListView listView_category;
    CategoryAdapter adapter;

    private List<ProductType> list = new ArrayList<>(

    );


    @Override
    public int getContentResouceId() {
        return R.layout.fragment_category;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        context = getActivity();
        setViews();
        setListener();
    }

    private void setViews() {
        getDatas();

        adapter = new CategoryAdapter(context);
        listView_category.setAdapter(adapter);
    }

    private void getDatas() {
        mHttpUtil.getCategoryData(new ProgressSubscriber<ReturnMsg>(context) {
            @Override
            public void next(ReturnMsg returnMsg) {
                switch (returnMsg.getCode()) {
                    case 0:

                        break;
                    case 1:
                        Type type = new TypeToken<List<ProductType>>() {
                        }.getType();
                        Object fromJson = new Gson().fromJson(returnMsg.getInfo(), type);
                        list = (ArrayList<ProductType>) fromJson;
                        adapter.setData(list);
                        break;
                }
            }
        }, "producttype");
    }

    private void setListener() {
        category_search.setOnClickListener(this);
    }

    @Override
    public String getFragmentName() {
        return null;
    }

    @Override
    public void onClick(View view) {
        Intent intent = null;
        switch (view.getId()) {
            case R.id.category_search:
                intent = new Intent(context, ProductSearchActivity.class);
                startActivity(intent);
                break;
        }
    }
}